package cn.edu.zjicm.wordsnet_d.util;

import java.io.File;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class BitmapUtil {

	// cropForWindow 是否按窗口大小裁剪图片
	public static BitmapDrawable getWallPaper(Activity context, boolean cropForWindow) {
		WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
		// 获取当前壁纸
		Drawable wallpaperDrawable = wallpaperManager.getDrawable();
		// 将Drawable,转成Bitmap
		Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();

		if (cropForWindow)
			return new BitmapDrawable(CropForWindow(bm, context));
		else
			return new BitmapDrawable(bm);
	}

	// 按屏幕大小剪切
	public static Bitmap CropForWindow(Bitmap bm, Activity context) {
		WindowManager manage = context.getWindowManager();
		Display display = manage.getDefaultDisplay();
		int screenHeight = display.getHeight();
		int screenWidth = display.getWidth();
		int width = bm.getHeight() * screenWidth / screenHeight;
		Bitmap pbm = null;
		try {
			if (bm.getWidth() >= width) {
				// pbm = Bitmap.createBitmap(bm, (bm.getWidth() - width) / 2, 0,
				// width, bm.getHeight());

				// 位图缩放到屏幕大小
				Matrix matrix = new Matrix();
				matrix.postScale((float) screenWidth / width, (float) screenHeight / bm.getHeight());
				pbm = Bitmap.createBitmap(bm, (bm.getWidth() - width) / 2, 0, width, bm.getHeight(), matrix, true);
			} else {
				int height = bm.getWidth() * screenHeight / screenWidth;
				// pbm = Bitmap.createBitmap(bm, 0, (bm.getHeight() - height) /
				// 2,
				// bm.getWidth(), height);

				// 位图缩放到屏幕大小
				Matrix matrix = new Matrix();
				matrix.postScale((float) screenWidth / bm.getWidth(), (float) screenHeight / height);
				pbm = Bitmap.createBitmap(bm, 0, (bm.getHeight() - height) / 2, bm.getWidth(), height, matrix, true);
			}
			// 出现以下异常，暂时注释 java.lang.RuntimeException: Canvas: trying to use a
			// recycled bitmap android.graphics.Bitmap@4202aff0
			// bm.recycle();

			return pbm;// Bitmap.createScaledBitmap(pbm, screenHeight,
						// screenWidth, true);
		} catch (OutOfMemoryError e) {
			return null;
		}
	}

	public static Bitmap CropForAvatar(Bitmap bm, Activity content) {
		Bitmap pbm = null;
		float imageWidth = 320;
		try {
			Matrix matrix = new Matrix();
			int minLength = Math.min(bm.getWidth(), bm.getHeight());
			matrix.postScale(imageWidth / minLength, imageWidth / minLength);
			bm = Bitmap.createBitmap(bm, 0, 0, (int) (bm.getWidth()), (int) (bm.getHeight()), matrix, true);// 将图片最短边设置为320
			pbm = Bitmap.createBitmap(bm, (int) ((bm.getWidth() - imageWidth) / 2), (int) ((bm.getHeight() - imageWidth) / 2), (int) (imageWidth), (int) (imageWidth), null, true);
			return pbm;
		} catch (OutOfMemoryError e) {
			return null;
		}
	}

	public static void saveAvatar(Bitmap avatar, Activity context) {
		String strAvatarFilename = "avatar.jpg";
		try {
			avatar.compress(CompressFormat.JPEG, 100, context.openFileOutput(strAvatarFilename, Context.MODE_PRIVATE));
//			File file1 = new File(Environment.getExternalStorageDirectory().toString()+"/1.jpg");
//			avatar.compress(CompressFormat.JPEG, 100, new BufferedOutputStream(new FileOutputStream(file1)));
		} catch (Exception e) {
			// Log.e(DEBUG_TAG, "Avatar compression and save failed.", e);
		} catch (OutOfMemoryError e) {
			Toast.makeText(context, "图像处理失败，请选择大小合适的图像", Toast.LENGTH_SHORT).show();
		}

		Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(context.getFilesDir(), strAvatarFilename));
		AppPreference.setLockScreenWallpaper(context, "file://" + imageUriToSaveCameraImageTo.getPath());
	}

	public static void saveHeadPortrait(Bitmap headPortrait, Context context) {
		String strHeadPortraitFilename = "head_portrait.jpg";
		try {
			headPortrait.compress(CompressFormat.JPEG, 100, context.openFileOutput(strHeadPortraitFilename, Context.MODE_PRIVATE));
		} catch (Exception e) {
		} catch (OutOfMemoryError e) {
			Toast.makeText(context, "图像处理失败，请选择大小合适的图像", Toast.LENGTH_SHORT).show();
		}
		Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(context.getFilesDir(), strHeadPortraitFilename));
		AppPreference.setHeadPortrait(context, "file://" + imageUriToSaveCameraImageTo.getPath());
	}

	public static void saveADs(Bitmap bitmap, Utils.welcomeADsIndex index, Context context) {
		if (bitmap == null) {
			savePics(context, null, index);
			return;
		}
		String name = "ads_priority" + index.toString() + ".jpg";
		try {
			bitmap.compress(CompressFormat.JPEG, 100, context.openFileOutput(name, Context.MODE_PRIVATE));
		} catch (Exception e) {
		}
		Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(context.getFilesDir(), name));
		savePics(context, "file://" + imageUriToSaveCameraImageTo.getPath(), index);
	}

	private static void savePics(Context context, String path, Utils.welcomeADsIndex index) {
		switch (index) {
		case ad1:
			AppPreference.setADs1(context, path);
			break;
		case ad2:
			AppPreference.setADs2(context, path);
			break;
		case ad3:
			AppPreference.setADs3(context, path);
			break;
		case adPro:
			AppPreference.setADSPriority(context, path);
			break;
		}
	}

	// public static void saveADsPriority(Bitmap bitmap, Context context) {
	// if (bitmap == null) {
	// AppPreference.setADSPriority(context, null);
	// return;
	// }
	// String name = "ads_priority.jpg";
	// try {
	// bitmap.compress(CompressFormat.JPEG, 100, context.openFileOutput(name,
	// Context.MODE_PRIVATE));
	// } catch (Exception e) {
	// }
	// Uri imageUriToSaveCameraImageTo = Uri.fromFile(new
	// File(context.getFilesDir(), name));
	// AppPreference.setADSPriority(context, "file://" +
	// imageUriToSaveCameraImageTo.getPath());
	// }

}
